<template>
  <!-- 头部区域 -->
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="@/assets/img/tx.jpeg" alt="" class="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <!-- 侧边栏菜单区域 -->
        <!-- default-active 高亮 属性 -->
        <!-- unique-opened  保持一个子菜单的展开 -->
        <!-- collapse-transition 是否开启折叠动画 -->
        <!-- collapse 开启折叠效果 -->

        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标区域 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本区域 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavSetate('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标区域 -->
                <i class="el-icon-menu"></i>
                <!-- 文本区域 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 页面主题部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      // 左侧菜单数据
      menuList: [],

      iconsObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("indexpath");
  },

  methods: {
    // 实现退出功能
    logout() {
      // 清空token
      window.sessionStorage.clear();
      // 重定向到login页面
      this.$router.push("/login");
    },
    saveNavSetate(activePath) {
      window.sessionStorage.setItem("indexpath", activePath);
      this.activePath = activePath;
    },

    // 获取所有的菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error("请求失败");
      } else {
        this.menuList = res.data;
      }
      // console.log(this.menuList)
    },
    // 点击按钮 实现菜单水平折叠功能
    toggle() {
      this.isCollapse = !this.isCollapse;
    },
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  padding-left: 0;
  background-color: #373d41;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 3px;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.logo {
  border-radius: 50%;
}
</style>
